vimfandomcom-20200223-history
Moving lines up or down
Programmers often want to move a line of text up or down, or to some other position. We start by explaining the basics (cut and paste, as well as move), and show how to append to a register. An amazing trick with the redo register is then presented (useful for reordering up to nine lines), and there is a set of mappings so you can press a key to move the current line, or a block of selected lines, up or down. Cut and paste Cut the line that you want to move by typing dd, or visually select some lines (press V then move the cursor) and type d to cut the selected block. Then move the cursor, and paste the text at the new position (press p to paste after the line with the cursor, or P to paste before). You can cut several lines, or blocks of text, by appending to a register using an uppercase letter, for example: *Move to the first line you wish to move; type "add to delete it to register a. *Move to another line; type "Add to delete it and append to the same register. *Move to another line; type "Add to delete it and append to the same register. **or type "." for the same effect *Select a range of lines vith V; type "Ad to delete the entire range and append it to the same register. *Continue in this fashion. *Move to the wanted destination. *Press "ap to paste after the line with the cursor, or "aP to paste before. Move command You can move a line, or a block of lines, with the :m command. Examples: For clarity, a space is shown after the :m commands above, but that space is not required. To move a block of lines, use the same command but visually select the lines before entering the move command. You can also use arbitrary ranges with the move command. Examples: Reordering up to nine lines The following example lines can be moved to a different order by deleting each line in turn (starting with the line that will be first when the move is complete): line 3 line 9 line 8 line 1 line 5 line 7 line 2 line 6 line 4 Move the cursor to "line 1" and type dd to delete the line. Go to "line 2" and press . to repeat (delete another line). Repeat this on "line 3", and so on, until everything has been deleted in order. Now type "1P to paste the contents of register 1 before the cursor. Repeat with the dot command, eight times: ........ The first dot command pastes register 2, and the next pastes register 3, and so on. The result is that all the lines are pasted, in the correct order. You have to press . eight times (using a count like 8. will insert the same line eight times). See Mappings to move lines The following mappings in your vimrc provide a quick way to move lines of text up or down. The mappings work in normal, insert and visual modes, allowing you to move the current line, or a selected block of lines. nnoremap :m .+1 nnoremap :m .-2 inoremap :m .+1 gi inoremap :m .-2 gi vnoremap :m '>+1gv=gv vnoremap :m '<-2gv=gv In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up. After visually selecting a block of lines (for example, by pressing V then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up. Explanation The command :m .+1 (which can be abbreviated to :m+) moves the current line to after line number .+1 (current line number + 1). That is, the current line is moved down one line. The command :m .-2 (which can be abbreviated to :m-2) moves the current line to after line number .-2 (current line number − 2). That is, the current line is moved up one line. After visually selecting some lines, entering :m '>+1 moves the selected lines to after line number '>+1 (one line after the last selected line; '> is a mark assigned by Vim to identify the selection end). That is, the block of selected lines is moved down one line. The re-indents the line to suit its new position. For the visual-mode mappings, gv reselects the last visual block and = re-indents that block. See also *Transposing enhanced mappings (better at maintaining cursor position?) *Cut or copy lines without counting the lines (needs to be fixed) *stackoverflow - Move entire line up and down in Vim *stackoverflow - Alt keys in Vim on OS X Comments